#include "Precomp.h"
#include "ltGUIHelper.h"
#include <boost/bind.hpp>
#include "ltInputManager.h"
#include "ltGraphicManager.h"

namespace Lotus
{
	LT_IMPLEMENT_SINGLETON(GUIHelper);

	GUIHelper::GUIHelper()
	{
		gui=new MyGUI::Gui();
		gui->initialise(GraphicManager::getInstance().getOgreRenderWindow(),"core.xml",Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,"Logs/MyGUI.log");
		GraphicManager::getInstance().FrameStartedEvent.connect(boost::bind(&GUIHelper::frameStarted,this,_1),*this);
		InputManager::getInstance().KeyPressedEvent.connect(boost::bind(&GUIHelper::keyPressed,this,_1),*this);
		InputManager::getInstance().KeyReleasedEvent.connect(boost::bind(&GUIHelper::keyReleased,this,_1),*this);
		InputManager::getInstance().MouseMovedEvent.connect(boost::bind(&GUIHelper::mouseMoved,this,_1),*this);
		InputManager::getInstance().MousePressedEvent.connect(boost::bind(&GUIHelper::mousePressed,this,_1,_2),*this);
		InputManager::getInstance().MouseReleasedEvent.connect(boost::bind(&GUIHelper::mouseReleased,this,_1,_2),*this);
		gui->setActiveViewport(0);
	}

	GUIHelper::~GUIHelper()
	{
		gui->shutdown();
		delete gui;
	}

	void GUIHelper::frameStarted(const Ogre::FrameEvent& evt)
	{
		gui->injectFrameEntered(evt.timeSinceLastFrame);
	}

	void GUIHelper::mouseMoved( const OIS::MouseEvent &arg )
	{
		gui->injectMouseMove(arg);
	}

	void GUIHelper::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
	{
		gui->injectMousePress(arg,id);
	}

	void GUIHelper::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
	{
		gui->injectMouseRelease(arg,id);
	}

	void GUIHelper::keyPressed( const OIS::KeyEvent &arg )
	{
		gui->injectKeyPress(arg);
	}

	void GUIHelper::keyReleased( const OIS::KeyEvent &arg )
	{
		gui->injectKeyRelease(arg);
	}

	MyGUI::Gui* GUIHelper::getGui() const
	{
		return gui;
	}
}